package com.monitor.filter;

import com.monitor.common.TPMonitorUtil;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;


@Activate(group = {CommonConstants.CONSUMER,CommonConstants.PROVIDER})

public class TPMonitorFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

        long   startTime  = System.currentTimeMillis();

        Result invoke = invoker.invoke(invocation);

        long endTime = System.currentTimeMillis();

        long runTime = endTime -startTime;

        if(invocation.getMethodName().equals("methodA")){
            TPMonitorUtil.perSecondRunTimeQueueA.offer(runTime);
        }else if(invocation.getMethodName().equals("methodB")){
            TPMonitorUtil.perSecondRunTimeQueueB.offer(runTime);
        }else if(invocation.getMethodName().equals("methodC")){
            TPMonitorUtil.perSecondRunTimeQueueC.offer(runTime);
        }

        try {
            // 执行方法
            return  invoke;
        } finally {
            //System.out.println(invocation.getMethodName()+"耗时时间:" + runTime + "毫秒");
        }

    }

}
